﻿using Kinlo.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Domains;
using UIWindows.Enums;
using XingLucifer.IBase.Enums;

namespace UIWindows.Tools.Helper
{
    public static class SqlHelper
    {

        /// <summary>
        /// 查询获取电池数据
        /// </summary>
        /// <param name="dateTime">查询时间</param>
        /// <param name="message">前称重 后称重等</param>
        /// <param name="snowflakeId">雪花ID</param>
        /// <returns></returns>
        public static ProductionData GetEntity(this DateTime dateTime, TaskTypes message, long snowflakeId)
        {
            ProductionData.Meta.TableName = $"ProductionData_{dateTime:yyyy}{dateTime.GetQuarterly()}";
            ProductionData entity = null;
            try
            {
                entity = ProductionData.Find(ProductionData._.SnowflakeId == snowflakeId);
            }
            catch (Exception ex)
            {
                $"{message} 雪花ID:{snowflakeId} 数据库操作错误：{ex}".RunLog(MessageLevelType.警告);
            }
            if (entity == null)
            {
                if (dateTime.ContrastTime())
                {
                    dateTime = dateTime.AddDays(-1);
                    return dateTime.GetEntity(message, snowflakeId);
                }
                $"{message} 找不到雪花ID为：[{snowflakeId}]".RunLog(MessageLevelType.警告);
            }
            return entity;
        }


    }
}
